from apps.common.base.views import *
from apps.pay.order.dao import OrderDao
from apps.merchant.item.dao import ItemDao
from django.db.models import Sum
from decimal import Decimal

class LargeDataViewSet(BaseViewSet):
    '''总体数据'''

    @action(methods=['post'], detail=False)
    def getTotalityData(self, request):
        params = getRequestParams(request)
        data = {}
        if request.user.role.key == 'superAdmin' or request.user.role.key == 'admin':
            data['shopName'] = '精英e站'
            data['incomes'] = 0
            # 订单
            orders = OrderDao().findAll()
            data['wait_send_count'] = orders.filter(orderStatus='WAIT_SEND').count()
            data['wait_pay_count'] = orders.filter(orderStatus='WAIT_PAY').count()
            # 商品
            items = ItemDao().findAll()
            data['selling_count'] = items.filter(checkStatus='PASS', status=1).count()
            data['stock_short_count'] = 0
        else:
            lessee = request.user.lessee
            data['shopName'] = lessee.name

            # 订单
            orders = OrderDao().findAll().filter(lessee=lessee)
            # 总营业额
            realPayAll = orders.filter(Q(orderStatus='WAIT_SEND') | Q(orderStatus='FINISH')).aggregate(sums=Sum('realPay'))
            data['moneyAll'] = str(Decimal(realPayAll['sums'])) if realPayAll['sums'] else 0

            data['wait_send_count'] = orders.filter(orderStatus='WAIT_SEND').count()
            data['wait_pay_count'] = orders.filter(orderStatus='WAIT_PAY').count()
            # 商品
            items = ItemDao().findAll().filter(lessee=lessee)
            data['selling_count'] = items.filter(checkStatus='PASS', status=1).count()
            data['stock_short_count'] = 0

        return Response(response_success(data))
